#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

function GenReleaseText()
{
	local relDir="$1"
	local hwVersion="$2"
	local swVersion="$3"
	local dir="$4"
	local application_url="$5"
	local version_file="$4/svn_version"
	local framework_version_file="${dir}/.version"
	local app__version_file="$dir/packages/.version"
	local framework_url=`svn.shc info  $4 |grep "URL:" |awk '{print $2}'`
echo "product_name=$product_name"
echo "hwVersion=$hwVersion"
echo "swVersion=$swVersion"
echo "======================================================================================================" > $relDir/${swVersion}_releaseNote.txt
echo "product_name:  $product_name
Hardware Version: $hwVersion
Software Version：$swVersion" >> $relDir/${swVersion}_releaseNote.txt
#cat >> $relDir/releaseNote.txt <<EOF
#------- $platform_name 主线 信息 -------# 
#EOF
	echo "framework_url=$framework_url"
	echo "application_url=$application_url"
	echo "version_file=$version_file"
	if test ! -f $framework_version_file; then
		echo "没有workcopy/.version文件，无法自动输出log信息，请检查工作目录"
		exit
	fi
	if test ! -f $app__version_file; then
		echo "没有workcopy/packages/.version文件，无法自动输出log信息，请检查工作目录"
		exit
	fi
	if test ! -f $version_file;then
		echo "svn_version文件不存在，创建初始文件，写入初始版本号"
		
		frame_ver_init=`sh_get_svn_committed_no_from_copy $dir`
		app_ver_init=`cat $app__version_file`
		echo "frame_ver_init=$frame_ver_init"
		echo "app_ver_init=$app_ver_init"
		touch $version_file
		echo "pre_framework_version:$frame_ver_init
pre_app_version:$app_ver_init
current_framework_version:$frame_ver_init
current_app_version:$app_ver_init
modem_version:init">>$version_file

	fi
	pre_framework_version=`cat $version_file | grep pre_framework_version | awk -F ':' '{print $2}'`
	pre_app_version=`cat $version_file | grep pre_app_version | awk -F ':' '{print $2}'`
	current_framework_version=`cat $version_file | grep current_framework_version | awk -F ':' '{print $2}'`
	current_app_version=`cat $version_file | grep current_app_version | awk -F ':' '{print $2}'`

	echo "pre_framework_version:$pre_framework_version"
	echo "pre_app_version:$pre_app_version"
	echo "current_framework_version:$current_framework_version"
	echo "current_app_version:$current_app_version"
	if [ -z $pre_framework_version ] || [ -z $pre_app_version ] || [ -z $current_framework_version ] || [ -z $current_app_version ];then
		echo "有版本号为空，请检查svn_version文件内容"
		exit
	fi

	echo "fs.get-whatsnew-by-rev.sh -b $framework_url -r ${pre_framework_version}:${current_framework_version} -o $relDir/${swVersion}_releaseNote.txt"
	echo "==========================================framework_log==================================================" >>  $relDir/${swVersion}_releaseNote.txt
	fs.get-whatsnew-by-rev.sh -b $framework_url -r ${pre_framework_version}:${current_framework_version} -o $relDir/${swVersion}_releaseNote.txt
	echo "=============================================app_log=====================================================" >>  $relDir/${swVersion}_releaseNote.txt
	echo "fs.get-whatsnew-by-rev.sh -b $application_url -r ${pre_app_version}:${current_app_version} -o $relDir/${swVersion}_releaseNote.txt"
	fs.get-whatsnew-by-rev.sh -b $application_url -r ${pre_app_version}:${current_app_version} -o $relDir/${swVersion}_releaseNote.txt
	sed -i "/pre_framework_version/s/.*/pre_framework_version:${current_framework_version}/g" $version_file
	sed -i "/pre_app_version/s/.*/pre_app_version:${current_app_version}/g" $version_file

}


function GenReleaseTextFromProduct()
{
	local relDir="$1"
	local hwVersion="$2"
	local swVersion="$3"
	local dir="$4"
	local application_url_product="$5"
	local framework_url_product=
	local framework_url_root=
	local application_url_root=
	local svn_version_key=
	local frame_svn_versions=
	local app_svn_versions=
echo "product_name=$product_name"
echo "hwVersion=$hwVersion"
echo "swVersion=$swVersion"
echo "======================================================================================================" > $relDir/${swVersion}_releaseNote.txt
echo "product_name:  $product_name
Hardware Version: $hwVersion
Software Version：$swVersion" >> $relDir/${swVersion}_releaseNote.txt
#cat >> $relDir/releaseNote.txt <<EOF
#------- $platform_name 主线 信息 -------# 
#EOF
	svn_version_key=`echo $swVersion | awk -F "_" '{print $3}'`
	framework_url_product=`sh_get_svn_url $dir`
	framework_url_root=`sh_get_svn_url_root $dir`
	application_url_root=`svn.shc info  $application_url_product | sed -n '3p' |awk '{print $2}'`
	echo "framework_url_product=$framework_url_product"
	echo "framework_url_root=$framework_url_root"
	echo "application_url_product=$application_url_product"
	echo "application_url_root=$application_url_root"
	echo "svn_version_key=$svn_version_key"
	frame_svn_versions=`sh_get_all_svn_log_info $framework_url_product $svn_version_key`
	app_svn_versions=`sh_get_all_svn_log_info $application_url_product $svn_version_key`
	echo "frame_svn_versions=$frame_svn_versions"
	echo "app_svn_versions=$app_svn_versions"
	
	echo "==========================================framework_log==================================================" >>  $relDir/${swVersion}_releaseNote.txt
	echo "fs.get-whatsnew-by-rev.sh -b $framework_url_root -r $frame_svn_versions -o $relDir/${swVersion}_releaseNote.txt"
	fs.get-whatsnew-by-rev.sh -b $framework_url_root -r $frame_svn_versions -o $relDir/${swVersion}_releaseNote.txt
	echo "=============================================app_log=====================================================" >>  $relDir/${swVersion}_releaseNote.txt
	echo "fs.get-whatsnew-by-rev.sh -b $application_url_root -r $app_svn_versions -o $relDir/${swVersion}_releaseNote.txt"
	fs.get-whatsnew-by-rev.sh -b $application_url_root -r $app_svn_versions -o $relDir/${swVersion}_releaseNote.txt
}


function save_compiler_info()
{
  local work_copy=$1
  local product_name=$2
  local l_file=$3
  if test ! -f $3; then
    touch $l_file
  fi
  l_url=`sh_get_svn_url $work_copy`
  l_url=`echo $l_url|sed "s#http://##g"`
  parseini.shc $l_file  add  MT6575  $l_url `cat $work_copy/.version`
}


function process_mtk()
{
  echo "自动打包mtk......"
  local l_config_files=
  local l_output_dir=
  local l_ftp_dir=/mnt/smartftp/images
  local l_release_dir=~/workspace/gphone/release/$project_name/images
  local hwVersion=
  local swVersion=
  local l_ftp_path=
  local project_type=
  local source_apk_name=
  local result_apk_name=
  local search_result=
  echo "l_release_dir=$l_release_dir"
  echo "out/target/product/$product_name/system.img"

  echo "l_version=$l_version"
  echo "work_copy=$work_copy"
  if test -f $work_copy/out/target/product/$product_name/system.img; then
#判断apk是否编译完全
     echo "编译成功..检查apk编译结果是否完全"
	 source_apk_name=`$work_copy/build/tools/findleaves.py  --prune=.repo --prune=.git packages/ vendor/bbk/source/  vendor/bbk/common/googleapps Android.mk |xargs grep -E "^\s*LOCAL_PACKAGE_NAME" -w |awk -F:= '{print $2}'`
	 result_apk_name=`find.sh -d $work_copy/out/target/product/$product_name/system/`
	 echo "source_apk_name=$source_apk_name"
	 echo "result_apk_name=$result_apk_name"
	 for i in $source_apk_name;do
		search_result=`echo $result_apk_name | grep -ow $i`
		echo "search_result=$search_result"
		if [ -z "$search_result" ];then
			echo "package:$i 没有编译，软件不能发布，打包停止，请检查"
			return
		fi
	 done
	 echo "apk编译结果正确，没有发现遗漏，打包继续"
#判断apk是否编译完全
     l_output_dir=`sh_get_output_dirname_of_mtk_imgs $work_copy $product_name $mode` 
     sh_copy_mtk_output_files $work_copy  $product_name  $project_name $l_release_dir/$l_output_dir
     hwVersion=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ro.hardware.bbk | awk -F '=' '{print $2}'`
     swVersion=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ro.build.version.bbk | awk -F '=' '{print $2}'`
#输出log信息到当前版本的releasenote中。
	 project_type=`sh_get_svn_url $work_copy | grep trunk`
     if [ "$auto_log" = "auto_log" ];then
		if [ -z "$project_type" ];then
			echo "当前项目为产品线项目,生成log文件，调用GenReleaseTextFromProduct"
			echo "GenReleaseTextFromProduct $l_release_dir/$l_output_dir $hwVersion $swVersion $work_copy $app_url"
			GenReleaseTextFromProduct $l_release_dir/$l_output_dir $hwVersion $swVersion $work_copy $app_url
		else
			echo "当前项目为非产品线项目,生成log文件，调用GenReleaseText"
			echo "GenReleaseText $l_release_dir/$l_output_dir $hwVersion $swVersion $work_copy $app_url"
			GenReleaseText $l_release_dir/$l_output_dir $hwVersion $swVersion $work_copy $app_url
		fi
#输出log信息到当前版本的releasenote中。
     fi

     #打包操作
     if [ "$no_tar" = "false" ]; then
       cd $l_release_dir
       tar -cvzf $l_output_dir.tar.gz $l_output_dir --exclude=*.zip --exclude=rootboot.img
       cd -
       echoTextRed "$l_release_dir/$l_output_dir.tar.gz"
       #release 拷贝到服务器
       echo "swVersion=$swVersion"
       l_ftp_path=$l_ftp_dir/$project_name/DailyTest/${swVersion}
       if [ "$target_dir" != "" ]; then
	  l_ftp_path=$l_ftp_dir/$project_name/$target_dir/${swVersion}
       fi
       echo "l_ftp_path=$l_ftp_path"

       if [ "$no_upload" = "false" ]; then
         if `mount_point_is_busy $l_ftp_dir`; then
           if [ -n "$project_name" ]; then
		if ! test -d $l_ftp_path; then
		   mkdir -pv $l_ftp_path
		fi
		cp -rvf $l_release_dir/$l_output_dir.tar.gz $l_ftp_path
		cp -rvf $l_release_dir/$l_output_dir/bbk*ota*.zip $l_ftp_path/${swVersion}-update-full.zip
		cp -rvf $l_release_dir/$l_output_dir/apps.zip $l_ftp_path/apps.zip
           fi
         else
           echoTextRed "/opt is not moounted!! return"
           return;
         fi
        fi
     fi
  else
	echo "$work_copy/out/target/product/$product_name/system.img"
	echo "编译失败,此处增加代码发送信息给作者......"
	error_file=`ls $work_copy/out/target/product/*.log_err`
        error_file=`echo $error_file | sed "s/_err//"`
	cat $error_file | grep -B 100 "^[0-9]* 错误"
        cat $error_file | grep -B 100 "^错误 [0-9]*"
  fi
}


function process_qcom()
{
  echo "自动打包qcom......"
  local l_config_files=
  local l_output_dir=
  local l_ftp_dir=/mnt/smartftp/images
  local l_release_dir=~/workspace/gphone/release/$project_name/images
  local hwVersion=
  local swVersion=
  local l_ftp_path=
  echo "l_release_dir=$l_release_dir"
  echo "out/target/product/$product_name/system.img"


  echo "work_copy=$work_copy"

  if test -f $work_copy/out/target/product/$product_name/system.img; then
     echo "编译成功" 
     l_output_dir=`sh_get_output_dirname_of_qcom_imgs $work_copy $product_name $mode` 
	echo "l_output_dir=$l_output_dir"

     sh_copy_qcom_output_files $work_copy  $product_name  $l_release_dir/$l_output_dir
     hwVersion=`cat $work_copy/out/target/product/$product_name/system/build.prop |grep ^ro.hardware.bbk | awk -F '=' '{print $2}'`
     swVersion=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ^ro.build.version.bbk | awk -F '=' '{print $2}'`
     echo "hwVersion=$hwVersion"
     echo "swVersion=$swVersion"
#输出log信息到当前版本的releasenote中。
     if [ "$auto_log" = "auto_log" ];then
       echo "GenReleaseText $l_release_dir/$l_output_dir $product_name $hwVersion $swVersion"
       GenReleaseText $l_release_dir/$l_output_dir $hwVersion $swVersion $work_copy $app_url
#输出log信息到当前版本的releasenote中。
     fi
	return
     #打包操作
     if [ "$no_tar" = "false" ]; then
       cd $l_release_dir
       tar -cvzf $l_output_dir.tar.gz $l_output_dir --exclude=*.zip --exclude=rootboot.img
       cd -
       echoTextRed "$l_release_dir/$l_output_dir.tar.gz"
       #release 拷贝到服务器
       echo "swVersion=$swVersion"
       l_ftp_path=$l_ftp_dir/$project_name/DailyTest/${swVersion}
       if [ "$target_dir" != "" ]; then
	  l_ftp_path=$l_ftp_dir/$project_name/$target_dir/${swVersion}
       fi
       echo "l_ftp_path=$l_ftp_path"

       if [ "$no_upload" = "false" ]; then
         if `mount_point_is_busy $l_ftp_dir`; then
           if [ -n "$project_name" ]; then
		if ! test -d $l_ftp_path; then
		   mkdir -pv $l_ftp_path
		fi
		cp -rvf $l_release_dir/$l_output_dir.tar.gz $l_ftp_path
		cp -rvf $l_release_dir/$l_output_dir/bbk*ota*.zip $l_ftp_path/${swVersion}-update-full.zip
		cp -rvf $l_release_dir/$l_output_dir/apps.zip $l_ftp_path/apps.zip
           fi
         else
           echoTextRed "/opt is not moounted!! return"
           return;
         fi
        fi
     fi
  else
	echo "$work_copy/out/target/product/$product_name/system.img"
	echo "编译失败,此处增加代码发送信息给作者......"
  fi
}

#脚本入口
mode=auto
no_tar=false
no_upload=false
while test $# != 0
do
    case $1 in
	-m)
	    shift
	    mode=$1
	    ;; 
	-t)
	    shift
	    type=$1
	    ;; 
   	-h)
	    shift
	    hardware=$1
	    ;;
   	-p)
	    shift
	    project_name=$1
	    ;;
   	-d)
	    shift
	    product_name=$1
	    ;;
	-f)
	    shift
	    target_dir=$1
	    ;;
	-l)
	    shift
	    auto_log=$1
	    ;;
	--work-copy)
	    shift
	    work_copy=$1
	    ;;
	--work_copy)
	    shift
	    work_copy=$1
	    ;;
	--app_url)
	    shift
	    app_url=$1
	    ;;
   	--app-url)
	    shift
	    app_url=$1
	    ;;    
       --no-upload)
	    no_upload=true
	    ;;    
       --no-tar)
	    no_tar=true
	    ;;   
        *)
	   work_copy=$1
	   ;;
       esac
       shift
done

if [ -z "$work_copy" ]; then
  work_copy=`pwd`
fi

echo "mode=$mode"
if [ "$mode" != "auto" ]; then 
  result=`question "是否打包软件？" 1 no` 
  if [  "$result" = "no" ] ; then
    no_tar=true
  fi
  if [  "$no_tar" = "false" ] ; then
    result=`question "是否拷贝到服务器？" 1 no` 
    if [  "$result" = "no" ] ; then
      no_upload=true
    fi
  fi
fi
#获取项目类型
echo "获取项目类型..."
if [ -z "$project_type" ]; then
  if test -f $work_copy/makeMtk; then
    project_type=mtk
  else
    project_type=qcom
  fi
fi
process_$project_type $mode

